package com.esp.soccertutor.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.regex.Pattern;

import android.content.Context;
import android.telephony.TelephonyManager;

import com.esp.soccertutor.ui.bean.BeanCountry;

public class Utils {
	

	private static String strPattern  = ".+@.+\\.[a-z]+";
	public static final Pattern EMAIL_ADDRESS_PATTERN=Pattern.compile(strPattern);
	public static boolean checkEmail(String strEmail){
		try{
			if(strEmail!=null){
				
				Log.print("Utils | checkEmail ","checkEmail() ==> EMAIL :"+strEmail);
				return  EMAIL_ADDRESS_PATTERN.matcher(strEmail.trim()).matches();
			}
			
		}catch (Exception e) {
			
			Log.error("Utils | checkEmail ", e);
			e.printStackTrace();
			
		}
		return false;
	}
	
	
	public static String[] arrListToArray(ArrayList<Object> arrList,String strMName) {
		String[] objStr = null;
		Method m = null;
		try {
		    if (arrList != null) {
			objStr = new String[arrList.size()];
			for (int index = 0; index < arrList.size(); index++) {
			    Object obj = arrList.get(index);
			    m = obj.getClass().getMethod(strMName);
			    objStr[index] = (String) m.invoke(obj, new Object[0]);
			}
		    }

		} catch (Exception e) {
		    e.printStackTrace();
		}

		return objStr;

    }
  
	public static String[] arrListToArray(ArrayList<BeanCountry> arrList) 
	{
		String[] objStr = null;
		try 
		{
			if (arrList != null) 
			{
				objStr = new String[arrList.size()];
				int index = 0;
				for (BeanCountry obj : arrList) 
				{
					objStr[index] = obj.getCountryname();
					index ++;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objStr;

	}
	
    public static String getDeviceID(Context context) {
	return ((TelephonyManager) context
		.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    public static String readTxt(Context context,String xmlName)
	{
		
		try 
	    {
			InputStream inputStream = context.getAssets().open(xmlName);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
	    
	    	int i = inputStream.read();
	    	while (i != -1)
	    	{
	    		byteArrayOutputStream.write(i);
	    		i = inputStream.read();
	    	}
	    	inputStream.close();
	    	
		    return byteArrayOutputStream.toString();
	    } 
	    catch (IOException e) 
	    {
	    	e.printStackTrace();
	    	return "";
	    }
	  
	}
    
    public static void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }

}
